/****************************************************************

kernel/driver/hdd.h
Hard disk drive driver header for AWOS

Copyright (c) 2006 Andrew Wilcox. All rights reserved.

Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.

3. The end-user documentation included with the redistribution,
if any, must include the following acknowledgment:

"This product includes software developed by Andrew Wilcox for
AWOS. (http://code.google.com/p/awos)."

Alternately, this acknowledgment may appear in the software
itself, if and wherever such third-party acknowledgments normally
appear.

4. The names "AWOS" and "Andrew Wilcox" must not be used to
endorse or promote products derived from this software without
prior written permission. For written permission, please contact
Andrew.R.Wilcox@Gmail.com.

5. Products derived from this software may not be called "AWOS"
 nor may "AWOS" appear in their name, without prior written
 permission of Andrew Wilcox.

THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL ANDREW WILCOX BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.

****************************************************************/

#ifndef ___DRIVER_HDD_H_
#define ___DRIVER_HDD_H_

/*functions*/
int HdReadSector(char, BOOL, size_t, long, char *);

/* structs */
struct KiAtapiIdent
{
	char config;
	char log_cyl;
	short reserved1[9];
	char serial[20];
	char vendorspec1[6];
	char firmware[8];
	char model[40];
	short vendorspec2;
	short reserved2;
	short cap;
	short unknown;
	short timing;
	short extended_valid;
	//0x6C-0x92 we don't care about
	short dontcare[19];
	short ver_major;
	short ver_minor;
	short reserved3[54];
	short venderspec3[32];
	short reserved4[96];
};

struct KiAtapiDisk
{
	BOOL exists;
	unsigned long long size;
	struct KiAtapiIdent ident;
};

struct KiIdeController
{
	struct KiAtapiDisk master;
	struct KiAtapiDisk slave;
};

struct KiDiskLayout
{
	struct KiIdeController pri;
	struct KiIdeController sec;
};

/* ports */
#define PORT_ATAPI_PRIBASE	0x1F0
#define PORT_ATAPI_SECBASE	0x170
#define PORT_ATAPI_DATA		0x0
#define PORT_ATAPI_ERROR	0x1
#define PORT_ATAPI_SECCOUNT	0x2
#define PORT_ATAPI_SECSTART	0x3
#define PORT_ATAPI_CYL_LO	0x4
#define PORT_ATAPI_CYL_HI	0x5
#define PORT_ATAPI_HEAD		0x6
#define PORT_ATAPI_COMMAND	0x7
#define PORT_ATAPI_STATUS	0x7

#define PORT_ATAPI_LBALOW	0x3
#define PORT_ATAPI_LBAMID	0x4
#define PORT_ATAPI_LBAHIGH	0x5
#define PORT_ATAPI_DRIVE	0x6

/* commands */

/* read */
#define ATAPI_READ_WITH_RETRY	0x20
#define ATAPI_READ_NO_RETRY	0x21
#define ATAPI_READL_WITH_RETRY	0x22
#define ATAPI_READL_NO_RETRY	0x23

/* write */
#define ATAPI_WRITE_WITH_RETRY	0x30
#define ATAPI_WRITE_NO_RETRY	0x31
#define ATAPI_WRITEL_WITH_RETRY	0x32
#define ATAPI_WRITEL_NO_RETRY	0x33

/* other */
#define ATAPI_FORMAT_TRACK	0x50

/*! \section status */
/** error -- if set, read the ERROR register to get the actual error **/
#define HDS_ERR			0x01
/** vendor-specific **/
#define HDS_IDX			0x02
/** correctable error -- if set, a correctable error has occurred. **/
#define HDS_CORR		0x04
/** data request -- if set, the drive is ready to send data **/
#define HDS_DRQ			0x08
/** device seek complete -- if set, the disk is settled on a track **/
#define HDS_DSC			0x10
/** device fault -- internal error (vendor-specific) **/
#define HDS_DF			0x20
/** drive ready -- if set, drive is ready for commands **/
#define HDS_DRDY		0x40
/** drive is busy -- if set, NO other bits are valid **/
#define HDS_BSY			0x80

/*! \section errors */
/**no error occurred**/
#define HDE_NOERROR		0x00
/**timeout waiting for response**/
#define HDE_TIMEOUT		0x01
/**no such track exists on the disk**/
#define HDE_NOSUCHTRACK		0x02
/**the IDE controller or disk aborted the command**/
#define HDE_ABORTED		0x03
/**ECC detected error**/
#define HDE_ECC			0x04
/**attempt to access bad block**/
#define HDE_BADBLOCK		0x05
/**no such drive exists on controller**/
#define HDE_INVALIDDRIVE	0x06
/**other or unknown error occurred**/
#define HDE_UNKNOWN		0xFF

#endif //#ifndef ___DRIVER_HDD_H_
